﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>bind函数实现</title>
</head>
<body>
    <script type="text/javascript">
        Function.prototype.meBind = function () {
            var bindThis = null
            var this_ = this
            if (arguments.length === 0) {
                bindThis = Object(null)
            } else {
                bindThis = Object(arguments[ 0 ])
            }

            var args = []
            if (arguments.length > 1) {
                args = Array.from(arguments).slice(1, arguments.length)
            }
            var fn = function () {
                this_.apply(bindThis, Array.from(arguments).concat(args))
            }
            fn.prototype = Object.create(this.prototype)
            return fn
        }
        var doSomething = function (name, age) {
            console.log(name, age)
        }
        doSomething = doSomething.meBind({
            name: 'xxx'
        }, 2)
        doSomething(4, 6)
    </script>
</body>
</html>
